/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.order;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduCourse;
import com.idenger.modules.edu.service.order.EduOrderDetailsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.common.utils.excel.ExportExcel;
import com.idenger.modules.edu.entity.enrolldetails.EduEnrollDetails;
import com.idenger.modules.edu.entity.order.EduOrder;
import com.idenger.modules.edu.entity.order.EduOrderDetails;
import com.idenger.modules.edu.entity.project.EduProject;
import com.idenger.modules.edu.entity.user.EduUser;
import com.idenger.modules.edu.service.course.EduCourseService;
import com.idenger.modules.edu.service.enroll.EduEnrollService;
import com.idenger.modules.edu.service.enrolldetails.EduEnrollDetailsService;
import com.idenger.modules.edu.service.order.EduOrderService;
import com.idenger.modules.edu.service.project.EduProjectService;
import com.idenger.modules.edu.service.user.EduUserService;

/**
 * 订单Controller
 * @author 兰正军
 * @version 2017-12-12
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/order/eduOrder")
public class EduOrderController extends BaseController {

	@Autowired
	private EduOrderService eduOrderService;
	
	@Autowired
	private EduUserService eduUserService;
	@Autowired
	private EduEnrollService eduEnrollService;
	
	@Autowired
	private EduEnrollDetailsService eduEnrollDetailsService;
	
	@Autowired
	private EduCourseService eduCourseService;
	
	@Autowired
	private EduProjectService eduProjectService;
	
	@Autowired
	private EduOrderDetailsService eduOrderDetailsService;
	@ModelAttribute
	public EduOrder get(@RequestParam(required=false) String id) {
		EduOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduOrderService.get(id);
		}
		if (entity == null){
			entity = new EduOrder();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:order:eduOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduOrder eduOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduOrder> page = eduOrderService.findListByDetails(new Page<EduOrder>(request, response), eduOrder); 
		model.addAttribute("page", page);
		model.addAttribute("startDate",eduOrder.getStartDate());
		model.addAttribute("endDate",eduOrder.getEndDate());
		model.addAttribute("tradeNo", eduOrder.getTradeNo());
		return "modules/edu/order/eduOrderList";
	}

	@RequiresPermissions("edu:order:eduOrder:view")
	@RequestMapping(value = "form")
	public String form(EduOrder eduOrder, Model model) {
		
		model.addAttribute("eduOrder", eduOrder);
		return "modules/edu/order/eduOrderForm";
	}
	@RequiresPermissions("edu:order:eduOrder:view")
	@RequestMapping(value = "get")
	public String get(EduOrder eduOrder, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser eduUser = eduUserService.get(eduOrder.getUserid());
		EduEnrollDetails details =eduEnrollDetailsService.getByOrderId(eduOrder.getId());
		List<EduCourse> eduCourses = eduCourseService.courseListByOrderId(eduOrder.getId());
		List<EduProject> eduProjects = eduProjectService.projectListByOrderId(eduOrder.getId());
		List<EduProject> projectList = new ArrayList<>();
		for(EduProject eduProject :eduProjects){
			List<EduCourse> courseList = eduCourseService.courseListByEnrollProjectId(eduProject.getId(), eduUser.getId());
			eduProject.setEduCourseList(courseList);
			projectList.add(eduProject);
		}
		EduOrderDetails eduOrderDetails = eduOrderDetailsService.findByOrderId(eduOrder.getId());
		Map map = new HashMap();
		map.put("eduUser", eduUser);
		map.put("details", details);
		map.put("courseList", eduCourses);
		map.put("eduProjectList", projectList);
		map.put("eduOrderDetails", eduOrderDetails);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(map);
		return renderString(response, baseResponseMsg);
	}
	

	@RequiresPermissions("edu:order:eduOrder:edit")
	@RequestMapping(value = "save")
	public String save(EduOrder eduOrder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eduOrder)){
			return form(eduOrder, model);
		}
		eduOrderService.save(eduOrder);
		addMessage(redirectAttributes, "保存订单成功");
		return "redirect:"+Global.getAdminPath()+"/edu/order/eduOrder/?repage";
	}
	
	@RequiresPermissions("edu:order:eduOrder:edit")
	@RequestMapping(value = "delete")
	public String delete(EduOrder eduOrder, RedirectAttributes redirectAttributes) {
		eduOrderService.delete(eduOrder);
		addMessage(redirectAttributes, "删除订单成功");
		return "redirect:"+Global.getAdminPath()+"/edu/order/eduOrder/?repage";
	}
	
	@RequestMapping(value = "export")
	public String export(EduOrder eduOrder, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response, Model model){
		try {
			String fileName = "订单列表"+new Date().getTime()+".xlsx";
			List<EduOrder> list = eduOrderService.findListByDetails(eduOrder);
			ExportExcel excel = new ExportExcel("订单列表",EduOrder.class);
			excel.setDataList(list);
			excel.write(response, fileName);
			excel.dispose();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	

}